﻿// See https://aka.ms/new-console-template for more information

//1、引用EF类库 
//  EntityFrameworkCore.SqlServer
//  EntityFrameworkCore.Tools
//2、定义实体类-对应表结构
//3、定义数据库上下文类 并继承 DBContext
//4、定义表对象及连接字符串
//5、重写OnConfiguring

using Newtonsoft.Json;
using S6_EFAccess.EF;
string connString = "Server=.\\SQLEXPRESS;DataBase=tz1989_fw;Trusted_Connection=true;";
TZ1989DBContext context = new TZ1989DBContext(connString);

//查询数据
//var list=context.Base_User.ToList();
//Console.WriteLine(JsonConvert.SerializeObject(list));

//查询一条数据
var user = context.Base_User.Where(t => t.Id == "015").FirstOrDefault();
Console.WriteLine(JsonConvert.SerializeObject(user));
//更新数据
//user.Age = 40;
//user.UpdateTime= DateTime.Now;
//int row=context.SaveChanges();
//Console.WriteLine(row);

//删除数据
var tran = context.BeginTransaction();
try
{
    context.Base_User.Remove(user);
    int row = context.SaveChanges();
    tran.Commit();
    Console.WriteLine(row);
}
catch (Exception)
{
    tran.Rollback();
    throw;
}





//添加数据
//context.Base_User.Add(new Base_User
//{
//    Id = "015",
//    Name = "武松",
//    Gender = 1,
//    Age = 35,
//    CreateTime = DateTime.Now
//});
//int row=context.SaveChanges();
//Console.WriteLine(row);
Console.ReadKey();


